﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Moq;
using MvcEx.Configuration;

namespace MvcEx.Tests
{
    [TestFixture]
    public class MvcExApplicationControllerBuilderSelectorTests
    {
        [Test]
        public void create_builder_from_configuration()
        {
            var testConfiguration = new MvcExConfiguration();
            testConfiguration.Id = "something";
            testConfiguration.Modules = new MvcExConfigurationModule[]{
                new MvcExConfigurationModule()
                { 
                    Name = "Test"
                }
            };
            var testModule = new MvcExModule()
            {
                Name = "Some Test",
                Assemblies = new MvcExModuleAssembly[] { }
            };

            var applicationMock = new Mock<IMvcExApplication>();
            applicationMock.Setup(x => x.GetActiveConfiguration()).Returns(testConfiguration);
            applicationMock.Setup(x => x.GetModuleById(It.IsAny<String>())).Returns(testModule);

            MvcExApplicationControllerBuilderSelector selector =
               new MvcExApplicationControllerBuilderSelector(applicationMock.Object);

            var builder = selector.GetActiveBuilder();

            Assert.NotNull(builder);    
        }

        [Test]
        public void create_builder_from_configuration_twice_verify_same_builder()
        {
            var testConfiguration = new MvcExConfiguration();
            testConfiguration.Id = "something";
            testConfiguration.Modules = new MvcExConfigurationModule[]{
                new MvcExConfigurationModule()
                { 
                    Name = "Test"
                }
            };
            var testModule = new MvcExModule()
            {
                Name = "Some Test",
                Assemblies = new MvcExModuleAssembly[] { }
            };

            var applicationMock = new Mock<IMvcExApplication>();
            applicationMock.Setup(x => x.GetActiveConfiguration()).Returns(testConfiguration);
            applicationMock.Setup(x => x.GetModuleById(It.IsAny<String>())).Returns(testModule);
            
            MvcExApplicationControllerBuilderSelector selector =
                new MvcExApplicationControllerBuilderSelector(applicationMock.Object);

            var builder = selector.GetActiveBuilder();

            Assert.NotNull(builder);

            var builder2 = selector.GetActiveBuilder();

            Assert.NotNull(builder);
            Assert.AreEqual(builder2, builder);
        }


        [Test]
        public void create_builder_from_configuration_and_flush_configuration()
        {
            var testConfiguration = new MvcExConfiguration();
            testConfiguration.Id = "something";
            testConfiguration.Modules = new MvcExConfigurationModule[]{
                new MvcExConfigurationModule()
                { 
                    Name = "Test"
                }
            };
            var testModule = new MvcExModule()
            {
                 Name = "Some Test",
                 Assemblies = new MvcExModuleAssembly[] {}
            };

            var applicationMock = new Mock<IMvcExApplication>();
            applicationMock.Setup(x => x.GetActiveConfiguration()).Returns(testConfiguration);
            applicationMock.Setup(x => x.GetModuleById(It.IsAny<String>())).Returns(testModule);

            MvcExApplicationControllerBuilderSelector selector =
                new MvcExApplicationControllerBuilderSelector(applicationMock.Object);

            var builder = selector.GetActiveBuilder();

            Assert.NotNull(builder);

            applicationMock.Raise(x=>x.ConfigurationChanged += null, EventArgs.Empty);

            var builder2 = selector.GetActiveBuilder();

            Assert.NotNull(builder);
            Assert.AreNotEqual(builder2, builder);
        }
    }
}
